package com.hl.yyx.modules.ums.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hl.yyx.common.exception.ApiException;
import com.hl.yyx.common.response.Response;
import com.hl.yyx.common.util.FileUtils;
import com.hl.yyx.common.util.IpUtil;
import com.hl.yyx.common.util.RequestHolder;
import com.hl.yyx.common.vo.PageParamsDTO;
import com.hl.yyx.dto.IdDTO;
import com.hl.yyx.modules.ums.dto.SourceDTO;
import com.hl.yyx.modules.ums.mapper.UmsSourceMapper;
import com.hl.yyx.modules.ums.model.UmsSource;
import com.hl.yyx.modules.ums.service.UmsSourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;

/**
 * <p>
 * 资源表 服务实现类
 * </p>
 *
 * @author hl243695czyn
 * @since 2025-03-26
 */
@Service
public class UmsSourceServiceImpl extends ServiceImpl<UmsSourceMapper, UmsSource> implements UmsSourceService {

    @Autowired
    private UmsSourceMapper sourceMapper;

    @Value("${file.save.path}")
    private String fileSavePath;

    @Override
    public Response pageList(PageParamsDTO pageParamsDTO) {
        return null;
    }

    @Override
    public Response create(UmsSource umsSource) {
        return null;
    }

    @Override
    public Response update(UmsSource umsSource) {
        return null;
    }

    @Override
    public Response delete(IdDTO idDTO) {
        return null;
    }

    @Override
    public Response view(IdDTO idDTO) {
        return null;
    }

    @Override
    public SourceDTO upload(MultipartFile file, String moduleName) {
        if (StrUtil.isBlank(moduleName) || "null".equals(moduleName)) {
            moduleName = "source-file";
        }
        String path = fileSavePath + moduleName;
        FileUtils fileUtil = new FileUtils(file);
        UmsSource source = createSource(moduleName, path, fileUtil);
        // 上传文件
        try {
            fileUtil.upload(path, source.getFileNewName());
        } catch (IOException e) {
            e.printStackTrace();
            throw new ApiException("文件上传失败！");
        }
        // 保存到数据库
        int insert = sourceMapper.insert(source);
        if (insert == 0) {
            throw new ApiException("保存文件信息失败！");
        }
        // 返回文件信息
        SourceDTO sourceDTO = new SourceDTO();
        BeanUtil.copyProperties(source, sourceDTO);
        return sourceDTO;
    }

    private UmsSource createSource(String moduleName, String path, FileUtils fileUtil) {
        UmsSource source = new UmsSource();
        String newName = moduleName + "_" + new Date().getTime() + "." + fileUtil.getSuffix();
        source.setFileType(0);
        source.setFileOriginalName(fileUtil.getFullName());
        source.setFileNewName(newName);
        source.setFileSize(fileUtil.getSize());
        source.setFileSource(path);
        source.setModuleName(moduleName);
        if (RequestHolder.currentUser() != null) {
            source.setOperator(RequestHolder.currentUser().getUsername());
            source.setOperatorIp(IpUtil.getIpAddr(RequestHolder.currentRequest()));
        } else {
            source.setOperator("system");
            source.setOperatorIp("");
        }
        source.setCreateTime(new Date());
        source.setUpdateTime(new Date());
        return source;
    }
}
